package com.sun.es.service;

import com.sun.es.entity.Book;
import com.sun.es.repository.ESBookRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class BookService {

    @Autowired
    private ESBookRepository esBookRepository;

    public void addBook(Book book) {
        try {
            esBookRepository.save(book);
        } catch (Exception e) {
            log.error(String.format("保存ES错误！%s", e.getMessage()));
        }
    }

    public List<Book> findAll() {
        List<Book> list = new ArrayList<>();
        Iterable<Book> all = esBookRepository.findAll();
        for (Book book : all) {
            list.add(book);
        }
        return list;
    }

    public void deleteAll() {
        esBookRepository.deleteAll();
    }

}
